//
//  SBBaseButton.m
//  SBAdaptiveTool
//
//  Created by user on 2018/9/10.
//  Copyright © 2018年 陈振旭. All rights reserved.
//

#import "SBBaseButton.h"

#import "AdaptiveHeader.h"

@implementation SBBaseButton

-(void)awakeFromNib {
    [super awakeFromNib];
    
    [self.titleLabel setFont:[UIFont fontWithDescriptor:[self.titleLabel.font fontDescriptor] size:SBScaleNumber(self.titleLabel.font.pointSize)]];
    
}


#pragma mark - 设置属性

- (void)setButtonStyle:(NSInteger)buttonStyle {
    _buttonStyle = buttonStyle;
    SBButtonStyle style = buttonStyle;
    [self initButtonWithStyle:style Spacing:10];
}

- (void)setSBBorderColor:(UIColor *)SBBorderColor {
    if (!SBBorderColor) return;
    _SBBorderColor = SBBorderColor;
    self.layer.borderColor = SBBorderColor.CGColor;
}

- (void)setSBCornerRadius:(CGFloat)SBCornerRadius {
    if (!SBCornerRadius) return;
    _SBCornerRadius = SBCornerRadius;
    self.layer.cornerRadius = SBScaleNumber(SBCornerRadius);
}

- (void)setSBBorderWidth:(CGFloat)SBBorderWidth {
    if (!SBBorderWidth) return;
    _SBBorderWidth = SBBorderWidth;
    self.layer.borderWidth = SBScaleNumber(SBBorderWidth);
}

- (void)setSBMasksToBounds:(BOOL)SBMasksToBounds {
    _SBMasksToBounds = SBMasksToBounds;
    self.layer.masksToBounds = SBMasksToBounds;
}

- (void)initButtonWithStyle:(SBButtonStyle)style Spacing:(CGFloat)spac {
    float  spacing = spac;//图片和文字的上下间距
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    CGSize textSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : self.titleLabel.font}];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    
    CGFloat    titleWidth = textSize.width;
    CGFloat    imageWidth = imageSize.width;
    
    switch (style) {
        case SBBaseButtonTitleTop:
            
            self.imageEdgeInsets = UIEdgeInsetsMake((totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, (totalHeight - titleSize.height), 0);
            
            break;
        case SBBaseButtonTitleLeft:
            
            self.imageEdgeInsets = UIEdgeInsetsMake(0, 0.0, 0.0, 0);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
            
            break;
        case SBBaseButtonTitleRight:
            
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imageWidth+spacing*0.5),0,(imageWidth+spacing*0.5))];
            [self setImageEdgeInsets:UIEdgeInsetsMake(0, (titleWidth+spacing*0.5),0,-(titleWidth+spacing*0.5))];
            
            break;
        case SBBaseButtonTitleBottom:
            
            self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
            
            
            break;
            
        default:
            return;
            break;
    }
    
}

@end
